//
//  LoginView.m
//  iTimeline
//
//  Created by Anh_Tran on 1/15/13.
//  Copyright (c) 2013 Mobioneer Co., Ltd. All rights reserved.
//

#import "LoginView.h"
#import "AppDelegate.h"


@interface LoginView ()

@end

@implementation LoginView
@synthesize lbcontent,lberror;
@synthesize ima1,ima2,ima3,ima4;
@synthesize tf1,tf2,tf3,tf4;
@synthesize count;


-(id)initWithNibName:(NSString *)nibNameOrNil action:(NSString *) nameAction passcode:(NSString*) passCode bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    action = nameAction;
    if (passCode !=nil) {
        passcode =passCode;
    }
    if (self) {
        if ([nameAction isEqualToString:@"setpass"]) {
            count = 2;
            title = @"Set PassCode";
            _enterPrompt= NSLocalizedString(@"Enter a passcode", nil);
            _confirmPrompt = NSLocalizedString(@"Re-enter your passcode", nil);
        }
        
        if ([nameAction isEqualToString:@"enterpass"]) {
            count =2;
            title = NSLocalizedString(@"Enter Passcode", nil);
            _enterPrompt = NSLocalizedString(@"Enter your passcode", nil);
        }
        
        if ([nameAction isEqualToString:@"changepass"]) {
            
            title = NSLocalizedString(@"Change Passcode", nil);
            //            count =1;
            //count ++;
            _changePrompt= NSLocalizedString(@"Enter your old passcode", nil);
            _enterPrompt= NSLocalizedString(@"Enter your new passcode", nil);
            _confirmPrompt = NSLocalizedString(@"Re-enter your new passcode", nil);
        }
        // Custom initialization
    }
    return self;
    
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    i = 0;
    j = 0;
    
    // [NSCursor hide];
    self.navigationItem.hidesBackButton = YES;
    // Do any additional setup after loading the view, typically from a nib[s
    self.title = title;
    lbcontent.textColor = [UIColor blueColor];
    lberror.textColor = [UIColor redColor];
    if (j>0) {
        NSString *error = [[NSString stringWithFormat:@"%i",j] stringByAppendingString:@" re-enter pass error"];
        lberror.text = error;
        lberror.hidden = NO;
    }
    if (count == 1) {
        lbcontent.text = _changePrompt;
        lbcontent.hidden =NO;
    }
    if (count ==2) {
        lbcontent.text = _enterPrompt;
        lbcontent.hidden =NO;
    }
    if (count ==3) {
        lbcontent.text =_confirmPrompt;
        lbcontent.hidden =NO;
    }
    [tf1 becomeFirstResponder];
    tf1.keyboardType = UIKeyboardTypeNumberPad;
    tf2.keyboardType = UIKeyboardTypeNumberPad;
    tf3.keyboardType = UIKeyboardTypeNumberPad;
    tf4.keyboardType = UIKeyboardTypeNumberPad;
    
}
-(void) cancel
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
    if (tf3 == textField) {
        [tf3 resignFirstResponder];
        [tf2 becomeFirstResponder];
    }
    return YES;
}
-(void)viewWillAppear:(BOOL)animated

{
    NSLog(@"quay lai");
    
    [tf1 becomeFirstResponder];
    tf1.text =@"";
    tf2.text =@"";
    tf3.text = @"";
    tf4.text = @"";
    ima1.hidden = YES;
    ima2.hidden = YES;
    ima3.hidden = YES;
    ima4.hidden = YES;
    //j++;
    if (i>0) {
        NSString *error = @" Passcode did not match. Try again !";
        lberror.font = [UIFont boldSystemFontOfSize:14];
        lberror.text = error;
        lberror.hidden = NO;
    }
    //    if (j>0) {
    //        NSString *error = [[NSString stringWithFormat:@"%i",j] stringByAppendingString:@" Wrong passcode. Try again!"];
    //
    //        lberror.text = error;
    //        lberror.hidden = NO;
    //    }
    //i++;
}

-(void)viewWillDisappear:(BOOL)animated
{
    lberror.hidden =YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSString *strPassword = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if( [strPassword length] > 0 ){
        
        textField.text = string;
        
        if (textField == tf1) {
            [tf1 resignFirstResponder];
            ima1.hidden = NO;
            [tf2 becomeFirstResponder];
            
        }
        if (textField == tf2) {
            [tf2 resignFirstResponder];
            ima2.hidden = NO;
            [tf3 becomeFirstResponder];
        }if (textField == tf3) {
            [tf3 resignFirstResponder];
            ima3.hidden = NO;
            [tf4 becomeFirstResponder];
        }
        if (textField == tf4) {
            [tf4 resignFirstResponder];
            ima4.hidden = NO;
            sleep(0.5);
            [self checkAction];
        }
        return NO;
    }
    return YES;
}
-(void) checkAction
{
    
    NSString *pass = [[[tf1.text stringByAppendingString:tf2.text] stringByAppendingString:tf3.text] stringByAppendingString:tf4.text];
    NSLog(@"passcode  = %@", passcode);
    NSUserDefaults *pre = [NSUserDefaults standardUserDefaults];
    NSLog(@"pass= %@" ,pass);
    if ([action isEqualToString:@"setpass"]) {
        //        NSUserDefaults *pre = [NSUserDefaults standardUserDefaults];
        if ((count == 3) && (!([pass isEqualToString:passcode]))) {
            // [pre setInteger:i forKey:@"count"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else
            if ([pass isEqualToString:passcode]) {
                
                [pre setObject:passcode forKey:@"pass"];
                [pre synchronize];
                AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
                [delegate login];
                //                Screen2 *sr = [[Screen2 alloc] initWithNibName:@"Screen2" bundle:nil];
                //                sr.title = @" chao ban";
                //
                //                //sr.navigationItem.hidesBackButton = YES;
                //                [self.navigationController pushViewController:sr animated:YES];
            }
            else
            {
                LoginView *sr = [[LoginView alloc] initWithNibName:@"LoginView" action:@"setpass" passcode:pass bundle:nil];
                sr.count = 3;
                i++;
                sr.navigationItem.hidesBackButton = YES;
                [self.navigationController pushViewController:sr animated:YES];
                NSLog(@"passcode = %@",passcode);
            }
    }
    
    if ([action isEqualToString:@"enterpass"]) {
        // NSUserDefaults *pre = [NSUserDefaults standardUserDefaults];
        NSString *passcodeget;
        if ([pre stringForKey:@"pass"]) {
            passcodeget = [pre stringForKey:@"pass"];
        }
        if (![pass isEqualToString:passcodeget]){
            i++;
            [tf1 becomeFirstResponder ];
            tf1.text =@"";
            tf2.text =@"";
            tf3.text =@"";
            tf4.text =@"";
            ima1.hidden = YES;
            ima2.hidden = YES;
            ima3.hidden = YES;
            ima4.hidden = YES;
            
            lberror.textColor = [UIColor redColor];
            NSString *error = [[NSString stringWithFormat:@"%i",i] stringByAppendingString:@" Wrong passcode. Try again!"];
            lberror.text = error;
            lberror.hidden = NO;
        }
        else
        {
            i = 0;
            
            
            AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
            [delegate login];
            
            //                Screen2 *sr = [[Screen2 alloc] initWithNibName:@"Screen2" bundle:nil];
            //                 sr.navigationItem.hidesBackButton = YES;
            //                [self.navigationController pushViewController:sr animated:YES];
        }
        
    }
    if ([action isEqualToString:@"changepass"]){
        title = @"Change Passcode";
        NSLog(@"%i",count);
        
        
        
        NSString *passcodeget;
        if ([pre stringForKey:@"pass"]) {
            passcodeget = [pre stringForKey:@"pass"];
            NSLog(@"passcodeget = %@",passcodeget);
        }
        if (count == 1) {
            
            if (![pass isEqualToString:passcodeget]) {
                NSLog(@"change pass");
                [tf1 becomeFirstResponder ];
                j++;
                NSString *error =  [[NSString stringWithFormat:@"%i",j] stringByAppendingString:@" Wrong passcode. Try again!"];
                lberror.text = error;
                lberror.hidden = NO;
                ima1.hidden = YES;
                ima2.hidden = YES;
                ima3.hidden = YES;
                ima4.hidden = YES;
                tf1.text =@"";
                tf2.text =@"";
                tf3.text =@"";
                tf4.text =@"";
            }
            
            if ([pass isEqualToString:passcodeget]) {
                LoginView *sr = [[LoginView alloc] initWithNibName:@"LoginView" action:@"changepass" passcode:pass bundle:nil];
                sr.count = 2;
                sr.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
                sr.navigationItem.hidesBackButton = YES;
                [self.navigationController pushViewController:sr animated:YES];
                
            }
        }
        
        if (count == 3) {
            j++;
            if ([pass isEqualToString:passcode]) {
                //count =2;
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"pass"];
                [[NSUserDefaults standardUserDefaults]synchronize ];
                [pre setObject:pass forKey:@"pass"];
                [pre synchronize];
                j=0;
                [self.navigationController popToRootViewControllerAnimated:YES];
                //                    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
            }
            else{
                
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        }
        if (count == 2) {
            j=0;
            if ([pass isEqualToString:passcode]) {
                [tf1 becomeFirstResponder ];
                //j++;
                
                
                NSString *error =@"Enter a different passcode. \n Cannot re-use the same passcode";
                lberror.numberOfLines =3;
                //  lberror.lineBreakMode = UILineBreakModeWordWrap;
                lberror.text = error;
                lberror.hidden = NO;
                
                tf1.text =@"";
                tf2.text =@"";
                tf3.text =@"";
                tf4.text =@"";
                ima1.hidden = YES;
                ima2.hidden = YES;
                ima3.hidden = YES;
                ima4.hidden = YES;
            }
            else
            {
                i++;
                LoginView *sr = [[LoginView alloc] initWithNibName:@"LoginView" action:@"changepass" passcode:pass bundle:nil];
                sr.count =3;
                //sr.count = count + 1;
                
                sr.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
                sr.navigationItem.hidesBackButton = YES;
                [self.navigationController pushViewController:sr animated:YES];
                
            }
        }
        
        NSLog(@"passcode = %@",passcode);
    }
    //        else{
    //            Screen2 *sr = [[Screen2 alloc] initWithNibName:@"Screen2" bundle:nil];
    //            [self.navigationController pushViewController:sr animated:YES];
    //        }
}
@end
